package me.sdevil507.platform.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.common.exceptions.KeywordExistedException;
import me.sdevil507.biz.common.exceptions.OrgNameEnExistedException;
import me.sdevil507.biz.common.exceptions.OrgNameZhExistedException;
import me.sdevil507.org.dto.*;
import me.sdevil507.org.mapper.OrgKeywordMapper;
import me.sdevil507.org.mapper.OrgMapper;
import me.sdevil507.org.mapper.OrgUserMapper;
import me.sdevil507.org.po.OrgKeywordPo;
import me.sdevil507.org.po.OrgPo;
import me.sdevil507.org.service.OrgService;
import me.sdevil507.org.service.OrgUserService;
import me.sdevil507.supports.jpa.PageConvertCallBack;
import me.sdevil507.supports.jpa.PageResultDTO;
import me.sdevil507.supports.jpa.PageResultGenerator;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.status.ApiStatusCode;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机构[OrgController]
 *
 * @author sdevil507
 * created on 2020-07-13 13:40:20
 */
@RestController
@RequestMapping("/api/plat/org")
@Slf4j
public class PlatOrgController {

    @Autowired
    private OrgService orgService;

    @Autowired
    private OrgMapper orgMapper;

    @Autowired
    private OrgUserMapper orgUserMapper;

    @Autowired
    private OrgKeywordMapper orgKeywordMapper;

    @Autowired
    private OrgUserService orgUserService;

    /**
     * 分页获取信息列表
     *
     * @param dto 聚合查询/分页/排序参数
     * @return 分页数据
     */
    @SuppressWarnings("Convert2Lambda")
    @RequiresPermissions("plat:org:view")
    @RequestMapping(value = "/readAllByPage", method = RequestMethod.POST)
    public ApiResultDTO readAllByPage(@RequestBody OrgMergeParamsDto dto) {
        Page<OrgPo> pagePos = orgService.readAllByPage(dto.getSearchDto(), dto.getPageDto(), dto.getSortDto());
        PageResultDTO<OrgResultDto> pageResultDto = PageResultGenerator.convert(pagePos, new PageConvertCallBack<OrgResultDto, OrgPo>() {
            @Override
            public List<OrgResultDto> exec(List<OrgPo> list) {
                return orgMapper.pos2dtos(list);
            }
        });
        for (OrgResultDto record : pageResultDto.getRecords()) {
            record.setPresetMgr(orgUserMapper.po2SimpleDto(orgUserService.getPresetMgr(record.getId())));
        }
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), pageResultDto);
    }

    /**
     * 获取所有机构
     */
    @RequestMapping(value = "/readAll", method = RequestMethod.POST)
    public ApiResultDTO readAll() {
        List<OrgResultDto> list = orgMapper.pos2dtos(orgService.readAll());
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), list);
    }

    /**
     * 创建
     *
     * @param dto 传输类
     * @return 执行反馈
     */
    @RequiresPermissions("plat:org:add")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ApiResultDTO create(@RequestBody OrgParamsDto dto) {
        try {
            orgService.create(dto);
        } catch (OrgNameZhExistedException e) {
            return ApiResultGenerator.create(ApiStatusCode.ORG_NAME_ZH_EXISTED.getCode(), ApiStatusCode.ORG_NAME_ZH_EXISTED.getDescription());
        } catch (OrgNameEnExistedException e) {
            return ApiResultGenerator.create(ApiStatusCode.ORG_NAME_EN_EXISTED.getCode(), ApiStatusCode.ORG_NAME_EN_EXISTED.getDescription());
        }
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 设置机构管理员
     *
     * @param dto 入参
     * @return 反馈
     */
    @RequiresPermissions("plat:org:set:mgr")
    @PostMapping(value = "/setOrgMgr")
    public ApiResultDTO setOrgMgr(@RequestBody OrgUserParamsDto dto) {
        orgUserService.setUserRel(dto);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 根据手机号查询全部账户列表
     *
     * @param dto 入参
     * @return 反馈
     */
    @PostMapping(value = "/readAllMgrs")
    public ApiResultDTO readAllMgrs(@RequestBody OrgUserParamsDto dto) {
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), orgUserMapper.pos2SimpleDtos(orgUserService.readAll(dto)));
    }

    /**
     * 更新
     *
     * @param dto 传输类
     * @return 执行反馈
     */
    @RequiresPermissions("plat:org:edit")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResultDTO update(@RequestBody OrgParamsDto dto) {
        try {
            orgService.update(dto);
        } catch (OrgNameZhExistedException e) {
            return ApiResultGenerator.create(ApiStatusCode.ORG_NAME_ZH_EXISTED.getCode(), ApiStatusCode.ORG_NAME_ZH_EXISTED.getDescription());
        } catch (OrgNameEnExistedException e) {
            return ApiResultGenerator.create(ApiStatusCode.ORG_NAME_EN_EXISTED.getCode(), ApiStatusCode.ORG_NAME_EN_EXISTED.getDescription());
        }
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 删除
     *
     * @param dto 传输类
     * @return 执行反馈
     */
    @RequiresPermissions("plat:org:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ApiResultDTO delete(@RequestBody OrgParamsDto dto) {
        orgService.delete(dto);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 启用/禁用
     *
     * @param dto 入参
     * @return 执行反馈
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public ApiResultDTO enable(@RequestBody OrgParamsDto dto) {
        orgService.enable(dto);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 获取机构关键词列表
     *
     * @param dto 入参
     * @return 关键词列表
     */
    @PostMapping(value = "/readAllKeywords")
    public ApiResultDTO readAllKeywords(@RequestBody OrgKeywordParamsDto dto) {
        List<OrgKeywordPo> pos = orgService.readAllKeywords(dto.getOrgId());
        List<OrgKeywordResultDto> dtos = orgKeywordMapper.pos2dtos(pos);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), dtos);
    }

    /**
     * 更新关键词数组
     *
     * @param dto 入参
     * @return 反馈
     */
    @RequiresPermissions("plat:org:set:keyword")
    @PostMapping(value = "/updateKeywords")
    public ApiResultDTO updateKeywords(@RequestBody OrgParamsDto dto) {
        orgService.updateKeywords(dto);
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 创建关键词
     *
     * @return 反馈
     */
    @RequiresPermissions("plat:org:set:keyword")
    @PostMapping(value = "/createKeyword")
    public ApiResultDTO createKeyword(@RequestBody OrgKeywordParamsDto dto) {
        try {
            orgService.createKeyword(dto);
        } catch (KeywordExistedException e) {
            return ApiResultGenerator.create(ApiStatusCode.KEYWORD_EXISTED.getCode(), ApiStatusCode.KEYWORD_EXISTED.getDescription());
        }
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

    /**
     * 删除关键词
     *
     * @param dto 入参
     * @return 反馈
     */
    @RequiresPermissions("plat:org:set:keyword")
    @PostMapping(value = "/deleteKeyword")
    public ApiResultDTO deleteKeyword(@RequestBody OrgKeywordParamsDto dto) {
        orgService.deleteKeyword(dto.getName());
        return ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
    }

}